using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace stsdev {
  class WorkflowTemplateBuilder {

    private static List<string> WorkflowTemplateNames = new List<string>();

    public static void CreateSequentialWorkflowTemplate(string WorkflowTemplateName) {

      // @WorkflowTemplateName.cs
      string WorkflowTemplateSourceFilePath = WorkflowTemplateName + ".cs";
      FileStream WorkflowTemplate_cs = File.Create(WorkflowTemplateSourceFilePath);
      StreamWriter WorkflowTemplate_cs_writer = new StreamWriter(WorkflowTemplate_cs);
      string WorkflowTemplate_content = Properties.Resources.SequentialWorkflow_cs;
      WorkflowTemplate_content = WorkflowTemplate_content.Replace("@SolutionName", SolutionBuilder.SolutionName);
      WorkflowTemplate_content = WorkflowTemplate_content.Replace("@WorkflowTemplateName", WorkflowTemplateName);
      WorkflowTemplate_cs_writer.Write(WorkflowTemplate_content);
      WorkflowTemplate_cs_writer.Dispose();
      WorkflowTemplate_cs.Dispose();
      ProjectFileBuilder.AddSourceFile(WorkflowTemplateSourceFilePath, SourceFileTypeEnum.Compile, true);

      // @WorkflowTemplateName.designer.cs
      string WorkflowTemplateDesignerSourceFilePath = WorkflowTemplateName + ".designer.cs";
      FileStream WorkflowTemplate_designer_cs = File.Create(WorkflowTemplateDesignerSourceFilePath);
      StreamWriter WorkflowTemplate_designer_cs_writer = new StreamWriter(WorkflowTemplate_designer_cs);
      string WorkflowTemplateDesigner_content = Properties.Resources.SequentialWorkflow_designer_cs;
      WorkflowTemplateDesigner_content = WorkflowTemplateDesigner_content.Replace("@SolutionName", SolutionBuilder.SolutionName);
      WorkflowTemplateDesigner_content = WorkflowTemplateDesigner_content.Replace("@WorkflowTemplateName", WorkflowTemplateName);
      WorkflowTemplate_designer_cs_writer.Write(WorkflowTemplateDesigner_content);
      WorkflowTemplate_designer_cs_writer.Dispose();
      WorkflowTemplate_designer_cs.Dispose();
      ProjectFileBuilder.AddSourceFile(WorkflowTemplateDesignerSourceFilePath, WorkflowTemplateSourceFilePath, SourceFileTypeEnum.Compile);

      // add template name to WorkflowTemplateNames collection
      WorkflowTemplateNames.Add(@WorkflowTemplateName);
    }

    public static void CreateStateMachineWorkflowTemplate(string WorkflowTemplateName) {

      // @WorkflowTemplateName.cs
      string WorkflowTemplateSourceFilePath = WorkflowTemplateName + ".cs";
      FileStream WorkflowTemplate_cs = File.Create(WorkflowTemplateSourceFilePath);
      StreamWriter WorkflowTemplate_cs_writer = new StreamWriter(WorkflowTemplate_cs);
      string WorkflowTemplate_content = Properties.Resources.StateMachineWorkflow_cs;
      WorkflowTemplate_content = WorkflowTemplate_content.Replace("@SolutionName", SolutionBuilder.SolutionName);
      WorkflowTemplate_content = WorkflowTemplate_content.Replace("@WorkflowTemplateName", WorkflowTemplateName);
      WorkflowTemplate_cs_writer.Write(WorkflowTemplate_content);
      WorkflowTemplate_cs_writer.Dispose();
      WorkflowTemplate_cs.Dispose();
      ProjectFileBuilder.AddSourceFile(WorkflowTemplateSourceFilePath, SourceFileTypeEnum.Compile, true);

      // @WorkflowTemplateName.designer.cs
      string WorkflowTemplateDesignerSourceFilePath = WorkflowTemplateName + ".designer.cs";
      FileStream WorkflowTemplate_designer_cs = File.Create(WorkflowTemplateDesignerSourceFilePath);
      StreamWriter WorkflowTemplate_designer_cs_writer = new StreamWriter(WorkflowTemplate_designer_cs);
      string WorkflowTemplateDesigner_content = Properties.Resources.StateMachineWorkflow_designer_cs;
      WorkflowTemplateDesigner_content = WorkflowTemplateDesigner_content.Replace("@SolutionName", SolutionBuilder.SolutionName);
      WorkflowTemplateDesigner_content = WorkflowTemplateDesigner_content.Replace("@WorkflowTemplateName", WorkflowTemplateName);
      WorkflowTemplate_designer_cs_writer.Write(WorkflowTemplateDesigner_content);
      WorkflowTemplate_designer_cs_writer.Dispose();
      WorkflowTemplate_designer_cs.Dispose();
      ProjectFileBuilder.AddSourceFile(WorkflowTemplateDesignerSourceFilePath, WorkflowTemplateSourceFilePath, SourceFileTypeEnum.Compile);


      //// @WorkflowTemplateName.layout
      //string WorkflowTemplateLayoutSourceFilePath = WorkflowTemplateName + ".layout";
      //FileStream WorkflowTemplate_layout = File.Create(WorkflowTemplateLayoutSourceFilePath);
      //StreamWriter WorkflowTemplate_layout_writer = new StreamWriter(WorkflowTemplate_layout);
      //string WorkflowTemplateLayout_content = Properties.Resources.StateMachineWorkflow_layout;
      //WorkflowTemplateLayout_content = WorkflowTemplateLayout_content.Replace("@SolutionName", SolutionBuilder.SolutionName);
      //WorkflowTemplateLayout_content = WorkflowTemplateLayout_content.Replace("@WorkflowTemplateName", WorkflowTemplateName);
      //WorkflowTemplate_layout_writer.Write(WorkflowTemplateLayout_content);
      //WorkflowTemplate_layout_writer.Dispose();
      //WorkflowTemplate_layout.Dispose();
      //ProjectFileBuilder.AddSourceFile(WorkflowTemplateLayoutSourceFilePath, WorkflowTemplateSourceFilePath, SourceFileTypeEnum.Compile);

      
      // add template name to WorkflowTemplateNames collection
      WorkflowTemplateNames.Add(@WorkflowTemplateName);
    }
    
    public static void CreateWorkflowTemplateFeature() {
      FeatureBuilder.CreateElementManifest("WorkflowTemplates.xml", ElementsWriterCallback);
      FeatureBuilder.Create(SolutionBuilder.SolutionName, FeatureScopeEnum.Site, false);
    }

    private static void ElementsWriterCallback(XmlWriter writer) {
      foreach (string WorkflowTemplateName in WorkflowTemplateNames) {
        writer.WriteStartElement("Workflow");
        writer.WriteAttributeString("Name", WorkflowTemplateName);
        writer.WriteAttributeString("Description", "A workflow template created with STSDEV");
        writer.WriteAttributeString("Id", Guid.NewGuid().ToString().ToUpper());
        writer.WriteAttributeString("CodeBesideAssembly", SolutionBuilder.FullAssemblyName);
        writer.WriteAttributeString("CodeBesideClass", SolutionBuilder.SolutionName +  "." + WorkflowTemplateName);
        writer.WriteEndElement(); // </Workflow>
      }
    }
  }
}
